﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DM4;

namespace DM4Client.AddEditForms
{
    public partial class FrmAddEditGroup : XtraForm
    {
        public int GroupId = DM4Consts.DefaultNULLId;
        public OperationResult Result = new OperationResult();
        public const string FrmNewGroupCaption = "New project adding";

        private Dictionary<string, string> _scripts;

        public FrmAddEditGroup(int grpId = DM4Consts.DefaultNULLId)
        {
            GroupId = grpId;
            InitializeComponent();
        }

        private void FrmAddEditGroup_Shown(object sender, EventArgs e)
        {
            XMLInfo xi = FrmMain.GetScriptList();
            DataTable dtScripts = XMLInfo.FillTable(xi);

            _scripts = sscore.Utils.ComboUtils.DataTableToDict(dtScripts, "scr_id", "scr_name");

            if (GroupId == DM4Consts.DefaultNULLId)
            {
                Text = FrmNewGroupCaption;
                chbIsPublicGroup.Enabled = true;
                Common.FillComboWithSetId(cbScript, _scripts);
            }
            else
            {
                chbIsPublicGroup.Enabled = false;
                DataRow[] drcoll = FrmMain.DtGroups.Select(String.Format("[{0}]={1}", CnGroups.GrpId, GroupId));
                if (drcoll.Length > 0)
                {
                    DataRow dr = drcoll[0];
                    Text = String.Format("Edit project: {0}", dr[CnGroups.GrpName]);
                    tbGroupName.Text = dr[CnGroups.GrpName].ToString();
                    tbDescription.Text = dr[CnGroups.GrpDescription].ToString();
                    //tbHomeDir.Text = dr[CnGroups.GrpHomedir].ToString();

                    chbIsPublicGroup.Checked = dr[CnGroups.GrpUsr] == DBNull.Value;
                    Common.FillComboWithSetId(cbScript, _scripts, dr[CnGroups.GrpScriptId].ToString());
                }
                else
                {
                    Text = FrmNewGroupCaption;
                }
            }

            tbGroupName.Focus();
        }

        private void btnOkClick(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(tbGroupName.Text))
                {
                    MessageBox.Show(this, "Project name is empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //if (String.IsNullOrEmpty(tbHomeDir.Text))
                //{
                //    MessageBox.Show(this, "Home directory is empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    return;
                //}

                if (GroupId == DM4Consts.DefaultNULLId)
                {
                    Result = FrmMain.AddGroup(chbIsPublicGroup.Checked
                      , tbGroupName.Text.Trim()
                      , tbDescription.Text.Trim()
                      , tbHomeDir.Text.Trim(), Convert.ToInt32(Common.GetIdByComboValue(cbScript, _scripts)));
                }
                else
                {
                    Result = FrmMain.EditGroup(GroupId, tbGroupName.Text.Trim()
                        , tbDescription.Text.Trim()
                        , tbHomeDir.Text.Trim(), Convert.ToInt32(Common.GetIdByComboValue(cbScript, _scripts)));
                }

                if (Result.Result > 0)
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show(this, Result.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                string m = GroupId == DM4Consts.DefaultNULLId
                    ? "Error of project creating"
                    : "Error of project editing";
                FrmMain.WriteError(m + Environment.NewLine + ex.Message);
                Close();
            }
        }

        private void tbHomeDirButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (fb.ShowDialog() == DialogResult.OK)
                tbHomeDir.Text = fb.SelectedPath;
        }

    }
}